package zhieasy.cn.service.common.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhieasy.cn.model.common.WechatUser;
import zhieasy.cn.persistence.mapper.common.WeChatUserMapper;
import zhieasy.cn.service.common.WechatUserRoleService;
import zhieasy.cn.service.common.WechatUserService;

import java.util.Date;

/**
 * @author DaHang
 * @date 2020/8/13 15:36
 */
@Slf4j
@Service("wechatService")
public class WechatUserServiceImpl extends ServiceImpl<WeChatUserMapper, WechatUser> implements WechatUserService {

    @Autowired
    WeChatUserMapper weChatUserMapper ;

    @Autowired
    WechatUserRoleService wechatUserRoleService;

    /**
     * 通过openId查询有没有这个人
     * @param openId
     * @return WechatUser
     */
    @Override
    public WechatUser getWechatUserByOpenID(String openId) {
        QueryWrapper<WechatUser> query = new QueryWrapper<>();
        query.lambda().eq(WechatUser::getWxOpenId,openId);
        WechatUser user = this.baseMapper.selectOne(query);
        return user;
    }

    @Override

    public boolean insertWechatUser(WechatUser wechatUser) {
        QueryWrapper<WechatUser> query = new QueryWrapper<>();
        query.lambda().eq(WechatUser::getWxOpenId, wechatUser.getWxOpenId());
        //查询用户是否存在
        WechatUser one = weChatUserMapper.selectOne(query);
        wechatUser.setCreateTime(new Date().getTime());
        wechatUser.setUpdateTime(new Date().getTime());
        if (one != null) {
            return false;
        }
        int insert = weChatUserMapper.insert(wechatUser);
        //把这个人的信息都存起来，并且设置一下权限这些的
        boolean b = wechatUserRoleService.initWechatUserRole(wechatUser);
        return insert == 1 && b;
    }


}
